package com.example.dragon.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dragon.adpter.OrdersTabAdpter;
import com.example.dragon.view.MyFragment;
import com.example.dragon.view.MyViewPager;
import com.example.dragon.xmf.R;

import java.util.ArrayList;
import java.util.List;


public class OrdersFragment extends Fragment {

    private TabLayout mordersTablyout;                            //定义TabLayout
    private ViewPager mtabViewPager;                             //定义viewPager
    private FragmentPagerAdapter mtabAdpter;                               //定义adapter

    private List<Fragment> mlistfragment;                                //定义要装fragment的列表
    private List<String> mlistTitle;                                     //tab名称列表

    private PendingServiceFragment mPSFragment;              //待服务fragment
    private CanceledFragment mCFragment;            //取消fragment
    private InServiceFragment mISFragment;                      //服务中fragment
    private CompletedFragment mCLFragment;//已完成Fragment


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_orders,null);
        initControls(view);
        return view;
    }

    private void initControls(View view) {
        LinearLayout acbar = (LinearLayout)view.findViewById(R.id.acbar);
        TextView acbTitle = (TextView)view.findViewById(R.id.acbTitle);
        acbTitle.setText("订单管理");
        acbar.setPadding(60,60,60,60);
        acbTitle.setTextColor(R.color.mainColor);
        acbar.setBackgroundResource(R.drawable.border);
        mordersTablyout = (TabLayout)view.findViewById(R.id.mordersTab);
        mtabViewPager = (ViewPager)view.findViewById(R.id.mordersViewpage);

        //初始化各fragment
        mPSFragment = new PendingServiceFragment();
        mCFragment = new CanceledFragment();
        mCLFragment = new CompletedFragment();
        mISFragment = new InServiceFragment();

        //将fragment装进列表中
        mlistfragment = new ArrayList<>();
        mlistfragment.add(mPSFragment);
        mlistfragment.add(mISFragment);
        mlistfragment.add(mCLFragment);
        mlistfragment.add(mCFragment);


        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        mlistTitle = new ArrayList<>();
        mlistTitle.add("待服务");
        mlistTitle.add("服务中");
        mlistTitle.add("已完成");
        mlistTitle.add("已取消");

        //设置TabLayout的模式
        mordersTablyout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        mordersTablyout.addTab(mordersTablyout.newTab().setText(mlistTitle.get(0)));
        mordersTablyout.addTab(mordersTablyout.newTab().setText(mlistTitle.get(1)));
        mordersTablyout.addTab(mordersTablyout.newTab().setText(mlistTitle.get(2)));
        mordersTablyout.addTab(mordersTablyout.newTab().setText(mlistTitle.get(3)));

        mtabAdpter = new OrdersTabAdpter(getChildFragmentManager(),mlistfragment,mlistTitle);

        //viewpager加载adapter
        mtabViewPager.setAdapter(mtabAdpter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        mordersTablyout.setupWithViewPager(mtabViewPager);
        //tab_FindFragment_title.set
    }

    // TODO: Rename method, update argument and hook method into UI event
}
